//AI相关接口

import 'package:xvideos/hj_model/list_base_model.dart';
import 'package:xvideos/hj_utils/http_manager.dart';

import '../../hj_page/mine_business/ai/mode/AiChangeFaceVideoMod.dart';
import '../../hj_page/mine_business/ai/mode/AiModList.dart';
import '../../hj_page/mine_business/ai/mode/a_i_face_data.dart';
import '../../hj_page/mine_business/ai/mode/a_i_face_discount_entity.dart';
import '../../hj_page/mine_business/ai/mode/ai_gril_resp.dart';
import '../../hj_page/mine_business/ai/mode/ai_record_entity.dart';

class AIService {
  // 获取AI脱衣记录列表
  //status 1、进行中 2、生成成功 3、生成失败
  static Future<ListBaseModel<AiRecordModel>?> getAiRecordList(
    int? pageNumber,
    int? pageSize,
    int? status,
  ) async {
    final param = {'pageNumber': pageNumber, 'pageSize': pageSize, 'status': status};
    final result = await httpManager.fetchResponseByGET(
      '/ai/undress/list',
      param: param,
      jsonTransformation: (json) => ListBaseModel<AiRecordModel>.fromJson(json),
    );
    return result.data;
  }

  /// 获取AI照片换脸记录列表
  // 1、进行中 2、生成成功 3、生成失败
  static Future<ListBaseModel<AiRecordModel>?> getAiImgFaceRecordList(
    int? pageNumber,
    int? pageSize,
    int? status,
  ) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'status': status,
    };
    final result = await httpManager.fetchResponseByGET(
      '/ai/img/list',
      param: param,
      jsonTransformation: (json) => ListBaseModel<AiRecordModel>.fromJson(json),
    );
    return result.data;
  }

  /// 获取AI视频换脸记录列表
  // 1、进行中 2、生成成功 3、生成失败
  static Future<ListBaseModel<AiRecordModel>?> getAiFaceRecordList(
    int? pageNumber,
    int? pageSize,
    int? status,
  ) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'status': status,
    };
    final result = await httpManager.fetchResponseByGET(
      '/ai/changeface/list',
      param: param,
      jsonTransformation: (json) => ListBaseModel<AiRecordModel>.fromJson(json),
    );
    return result.data;
  }

  //生成AI脱衣记录
  static Future<bool> aiGenerate(
    List<String>? originPics, {
    List<String>? maskPic,
  }) async {
    final param = {'originPic': originPics, 'maskPic': maskPic}..removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByPOST(
      '/ai/undress/generate',
      param: param,
    );
    return result.isSuccess;
  }

  //生成AI视频换脸
  static Future<bool> aiFaceGenerate(
    List<String>? picture,
    String? vidModId,
    String? discountId,
  ) async {
    final param = {
      'picture': picture,
      'vidModId': vidModId,
      'discount': discountId?.isNotEmpty == true ? [discountId!] : null,
    }..removeWhere((key, value) => value == null);

    final result = await httpManager.fetchResponseByPOST('/ai/changeface/generate', param: param);
    return result.data == 'success';
  }

  //生成图片AI换脸
  static Future<bool> aiFacegenerateByPicture(String picture, String imgModId, {String? discount}) async {
    final param = {'originPic': picture, 'mId': imgModId, 'discount': discount};
    final result = await httpManager.fetchResponseByPOST(
      '/ai/img/generate',
      param: param,
    );
    return result.data == 'success';
  }

  static Future<ListBaseModel<AiChangeFaceVideoMod>?> getAIImgModelList(int pageNumber, int pageSize, {int? status}) async {
    final param = {'pageNumber': pageNumber, 'pageSize': pageSize, 'status': status};
    final result = await httpManager.fetchResponseByGET(
      '/ai/img/list',
      param: param,
      jsonTransformation: (json) => ListBaseModel<AiChangeFaceVideoMod>.fromJson(json),
    );
    return result.data;
  }

  //ai脱衣
  static Future<AiModList?> getAIModelList() async {
    final result = await httpManager.fetchResponseByGET(
      '/ai/mod/list',
      param: {},
      jsonTransformation: (json) => AiModList.fromJson(json),
    );
    return result.data;
  }

  //ai换脸 新版本
  static Future<AiChangeModList?> getAIChangeFaceList({
    int? type = 0, //0-图片换脸 1-视频换脸
    String? categoryId, //模版分类id，如果为空，则默认第一个模版分类
  }) async {
    final param = {'type': type, 'categoryId': categoryId}..removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/ai/mod/v2/list',
      param: param,
      jsonTransformation: (json) => AiChangeModList.fromJson(json),
    );
    return result.data;
  }

  //ai模版列表
  static Future<AiFaceData?> getAIFaceModel(
    int? pageNumber,
    int? pageSize,
  ) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
    };
    final result = await httpManager.fetchResponseByGET(
      '/ai/changeface/mod/list',
      param: param,
      jsonTransformation: (json) => AiFaceData.fromJson(json),
    );
    return result.data;
  }

  ///删除AI订单脱衣
  static Future<bool> deleteAiBill(String? id) async {
    final param = {'id': id};
    final result = await httpManager.fetchResponseByPOST('/ai/undress/hide', param: param);
    return result.isSuccess;
  }

  ///删除AI换脸Video订单
  static Future<bool> deleteAiFaceBill(String? id) async {
    final param = {'id': id};
    final result = await httpManager.fetchResponseByPOST('/ai/changeface/hide', param: param);
    return result.isSuccess;
  }

  ///删除AI换脸 Img订单
  static Future<bool> deleteAiFaceImgBill(String? id) async {
    final param = {'id': id};
    final result = await httpManager.fetchResponseByPOST('/ai/img/hide', param: param);
    return result.isSuccess;
  }

  //ai优惠券
  static Future<List<AIFaceDiscountEntity>> getAiFaceCoupon() async {
    final result = await httpManager.fetchResponseByGET(
      '/ai/changeface/coupon',
      param: {},
    );
    final List<AIFaceDiscountEntity> list = [];
    if (result.isSuccess) {
      List? values = result.data["detail"] as List?;
      list.addAll(values?.map((e) => AIFaceDiscountEntity.fromMap(e)).toList() ?? []);
    }
    return list;
  }

  //ai女友用户余额查询
  static Future<AIGirlFriendBalanceModel> getAiBalance(data) async {
    final result = await httpManager.fetchResponseByGET(
      '/aimate/getBalance',
      param: {},
      jsonTransformation: (value) => AIGirlFriendBalanceModel.fromJson(value),
    );
    return result.data;
  }

  //获取ai女友url
  static Future<AIGirlFriendUrlModel> getAiFriendUrl(data) async {
    final result = await httpManager.fetchResponseByGET(
      '/aimate/login',
      param: {},
      jsonTransformation: (value) => AIGirlFriendUrlModel.fromJson(value),
    );
    return result.data;
  }

  //获取AI女友货币列表
  static Future<AIGirlFriendCurrencys> getAiFriendCurrencys() async {
    final result = await httpManager.fetchResponseByGET(
      '/aimate/currencys',
      param: {},
      jsonTransformation: (value) => AIGirlFriendCurrencys.fromJson(value),
    );
    return result.data;
  }

  //AI女友积分兑换
  static Future<dynamic> exchangeAiFriend(data) async {
    final result = await httpManager.fetchResponseByPOST(
      '/aimate/exchange',
      param: data,
    );
    return result.data;
  }
}
